package com.example.springboot.listener;

import com.example.springboot.config.applicationContextNameAware.SpringContextHolder;
import com.example.springboot.utils.ApplicationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Set;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/9/27 20:47
 */
public class MyListener implements ServletContextListener {
    /**
     * 监控当前webserver启动
     * @param sce
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("web应用启动");
    }

    /**
     * 监控当前webserver销毁
     * @param sce
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        StringRedisTemplate stringRedisTemplate = (StringRedisTemplate)SpringContextHolder.getBean("stringRedisTemplate", StringRedisTemplate.class);
        // 删除统计请求次数的请求的key
        Set<String> keys = stringRedisTemplate.keys("/czk/**");
        // 删除这些可以值
        stringRedisTemplate.delete(keys);
        System.out.println("web应用销毁");
    }
}
